package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.NewsDaoImpl;
import com.ysd.entity.News;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminNewsServlet extends BaseServlet {

	public void getNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int newsId=0;
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId=Integer.parseInt(request.getParameter("newsId"));
		}
		int newsCategoryId=0;
		if(request.getParameter("newsCategoryId")!=null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId"));
		}
		String newsTitle = request.getParameter("newsTitle");
		String newsContent = request.getParameter("newsContent");
		
		News n = new News(newsId, newsTitle, newsContent, newsCategoryId, "");
		int page = 0;
		if(request.getParameter("page")!=null&&!request.getParameter("page").equals("")) {
			page=Integer.parseInt(request.getParameter("page"));
		}
		int size=0;
		if(request.getParameter("size")!=null&&!request.getParameter("size").contentEquals("")) {
			size=Integer.parseInt(request.getParameter("size"));
		}
		
		NewsDaoImpl ncDao = new NewsDaoImpl();
		List<News> nsList = ncDao.getAllNews(n,page,size);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String res = gson.toJson(nsList);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * 删除产品分类
	 * localhost:8080/kouhong/adminNews?reqName=delNews
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
		//接受前台传来的参数
		int newId=Integer.parseInt(request.getParameter("newId"));
		NewsDaoImpl	 pcDao = new NewsDaoImpl();
		
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out = response.getWriter();//out对像
		
		int n =pcDao.delnewsId(newId);
		if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", "因为"));
		}
		
		out.flush();
		out.close();
	}
	/**
	 * 添加资讯详情 localhost:8080/kouhong/adminNews?reqName=addNews
	 * 
	 * @param request
	 * @param response
	 * @param ne 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();// out对像
		int newsId = 0;
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));
		}
		
		String newsTitle=request.getParameter("newsTitle");
		String newsContent=request.getParameter("newsContent");
		int newsCategoryId=0;
		if(request.getParameter("newsCategoryId")!=null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId=Integer.parseInt(request.getParameter("newsCategoryId"));
		}
	
		News ne= new News(newsId, newsTitle, newsContent, newsCategoryId);
		NewsDaoImpl newss = new NewsDaoImpl();	
		int n=newss.addnews(ne);
		if (n>0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(true, "添加失败", ""));
		}
		out.flush();
		out.close();
	}
	
	
	
	/**
	 * localhost:8080/kouhong/adminNews?reqName=Newsedit
	 * 
	 * @param request
	 * @param response
	 * @param ne 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void Newsedit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();// out对像
		NewsDaoImpl pcDao = new NewsDaoImpl();	
		int nid = 0;
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			nid=Integer.parseInt(request.getParameter("newsId"));
		
		}
		System.out.println(nid);
		News pcc=	pcDao.getNewsId2(nid);
		
		Gson gson=new Gson();
		String res  =gson.toJson(pcc);
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 *  localhost:8080/kouhong/adminNews?reqName=upNews
	 * 
	 * @param request
	 * @param response
	 * @param ne 
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();// out对像
		NewsDaoImpl pcDao = new NewsDaoImpl();	
		int newsId = 0;
		if(request.getParameter("newsId")!=null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));
		}
		System.out.println(newsId);
		String newsTitle=request.getParameter("newsTitle");
		String newsContent=request.getParameter("newsContent");
		int newsCategoryId=0;
		if(request.getParameter("newsCategoryId")!=null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId=Integer.parseInt(request.getParameter("newsCategoryId"));
		}
	
		News ns=new News(newsId, newsTitle, newsContent, newsCategoryId);
		int n=pcDao.upNewsId(ns);
		if (n>0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}
		
		out.flush();
		out.close();
	}/**
	*kouhong/adminNews?reqName=getAllNews
	*
	*
	*
	*/
	public void getAllNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		NewsDaoImpl pcDao=new NewsDaoImpl();
		List<News> pcList=pcDao.getAllnewss();
		
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		Gson gson=new Gson();
		String res  =gson.toJson(pcList.size());
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	*kouhong/adminNews?reqName=delnews
	*
	*
	*
	*/
	public void delnews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//接受前台传来的参数
		response.setCharacterEncoding("UTF-8");//设置编码格式
		PrintWriter out = response.getWriter();//out对像
		
		NewsDaoImpl	 pcDao = new NewsDaoImpl();
		
		
		
		String ids = request.getParameter("ids");
		int n = pcDao.delNewsByid(ids);
	
		if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", "因为"));
		}
		
		out.flush();
		out.close();
	}
}



